package optimizer;

import midcode.MidCode;
import midcode.OPType;

import java.util.ArrayList;
import java.util.HashMap;

public class BasicBlock {
    private FuncBlock fatherBlock;
    private final ArrayList<MidCode> midCodes = new ArrayList<>();
    private final ArrayList<String> labels = new ArrayList<>();
    private final ArrayList<BasicBlock> preBlocks = new ArrayList<>();
    private final ArrayList<BasicBlock> nextBlocks = new ArrayList<>();
    private final HashMap<String, String> spreadMap = new HashMap<>();

    public BasicBlock() {
    }

    public BasicBlock(FuncBlock fatherBlock) {
        this.fatherBlock = fatherBlock;
    }

    public void addMidCode(MidCode midCode) {
        midCodes.add(midCode);
        midCode.setBasicBlock(this);
    }

    public void addLabel(String label) {
        labels.add(label);
    }

    public MidCode getLastMidCode() {
        return midCodes.get(midCodes.size() - 1);
    }

    public void addPreBlock(BasicBlock preBlock) {
        preBlocks.add(preBlock);
    }

    public void addNextBlock(BasicBlock postBlock) {
        nextBlocks.add(postBlock);
    }

//    public void setDefUseSet() {
//
//    }
//
//    public boolean setDefUseOutSet() {
//
//    }
//
//    public void setDefUseInSet() {
//
//    }
//
//    public void setDefUse() {
//
//    }

//    public void valSpread() {
//        for (MidCode midCode : midCodes) {
//            String val1 = midCode.getVal1();
//            String val2 = midCode.getVal2();
//            String dst = midCode.getDst();
//            switch (midCode.getOpType()) {
//                case PUSH_PARAM:
//                    if (spreadMap.containsKey(val1)) {
//                        midCode.setVal1(spreadMap.get(val1));
//                    }
//                    break;
//                case LW_ADDRESS:
//                case LW_ARRAY:
//                case SW_ARRAY:
//                    if (val2)
//            }
//        }
//    }
}

